package com.example.statistics.ui.sales

import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.text.style.UnderlineSpan
import android.view.View
import com.example.statistics.R
import com.example.statistics.bean.MarketSales
import com.example.statistics.common.BaseLoadingListActivity
import com.example.statistics.ex.addEx
import com.example.statistics.ex.getResColor
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.activityForResult
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector

class MarketSalesDailyListActivity : BaseLoadingListActivity<MarketSales>() {
    override val title: String
        get() = "商品流向日报"
    override val url: String
        get() = UrlConfig.PRODUCT_FLOW_LIST
    override val itemLayoutRes: Int
        get() = R.layout.list_item_purchase_product_prepary
    override val inputSearch: Boolean
        get() = true
    override val inputSearchFunc: (MarketSales, String) -> Boolean = { bean, str ->
        bean.driverPhone?.contains(str) == true ||
                bean.logisticsCompany?.contains(str) == true ||
                bean.salesArea?.contains(str) == true ||
                bean.plateNumber?.contains(str) == true
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        topBarRightImage {
            activityForResult<MarketSalesEditActivity>() {
                refresh()
            }
        }
    }

    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: MarketSales, position: Int) {
        val span = SpannableStringBuilder()
        //1
        var text = "${position + 1}. " + bean.salesArea
        var start = 0
        var end = text.length
        span.append(text).apply {
            setSpan(AbsoluteSizeSpan(36), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        }

        text = bean.customer.orEmpty()
        start = end
        end = end.addEx(text.length).toInt()
        span.append(text).apply {
            setSpan(ForegroundColorSpan(getResColor(R.color.lime_green)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            setSpan(AbsoluteSizeSpan(36), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        }

        text = bean.factoryNo + bean.productName
        start = end
        end = end.addEx(text.length).toInt()
        span.append(text).apply {
            setSpan(AbsoluteSizeSpan(36), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            setSpan(UnderlineSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        }

        text = bean.number.toString() + "件"
        start = end
        end = end.addEx(text.length).toInt()
        span.append(text).apply {
            setSpan(AbsoluteSizeSpan(36), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        }

        text = if (!bean.logisticsCompany.isNullOrBlank()) "(${bean.logisticsCompany}" else "(${bean.plateNumber} "
        start = end
        end = end.addEx(text.length).toInt()
        span.append(text).apply {
            setSpan(ForegroundColorSpan(Color.GRAY), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            setSpan(AbsoluteSizeSpan(30), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        }

        text = "${bean.driverPhone}"
        start = end
        end = end.addEx(text.length).toInt()
        span.append(text).apply {
            setSpan(ForegroundColorSpan(Color.BLUE), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            setSpan(AbsoluteSizeSpan(30), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        }

        text = ")"
        start = end
        end = end.addEx(text.length).toInt()
        span.append(text).apply {
            setSpan(ForegroundColorSpan(Color.GRAY), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            setSpan(AbsoluteSizeSpan(30), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        injector.text(R.id.tv_des, span)
                .longClicked {
                    adapter.remove(position)
                    false
                }

    }


}